<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜品编码" prop="dishCode">
      <el-input v-model="dataForm.dishCode" placeholder="菜品编码"></el-input>
    </el-form-item>
    <el-form-item label="能量" prop="energy">
      <el-input v-model="dataForm.energy" placeholder="能量"></el-input>
    </el-form-item>
    <el-form-item label="蛋白质" prop="protein">
      <el-input v-model="dataForm.protein" placeholder="蛋白质"></el-input>
    </el-form-item>
    <el-form-item label="碳水化合物" prop="carbohydrate">
      <el-input v-model="dataForm.carbohydrate" placeholder="碳水化合物"></el-input>
    </el-form-item>
    <el-form-item label="脂肪" prop="fat">
      <el-input v-model="dataForm.fat" placeholder="脂肪"></el-input>
    </el-form-item>
    <el-form-item label="糖" prop="sugar">
      <el-input v-model="dataForm.sugar" placeholder="糖"></el-input>
    </el-form-item>
    <el-form-item label="饱和脂肪" prop="saturatedFat">
      <el-input v-model="dataForm.saturatedFat" placeholder="饱和脂肪"></el-input>
    </el-form-item>
    <el-form-item label="反式脂肪" prop="transFat">
      <el-input v-model="dataForm.transFat" placeholder="反式脂肪"></el-input>
    </el-form-item>
    <el-form-item label="胆固醇" prop="cholesterol">
      <el-input v-model="dataForm.cholesterol" placeholder="胆固醇"></el-input>
    </el-form-item>
    <el-form-item label="钠" prop="sodium">
      <el-input v-model="dataForm.sodium" placeholder="钠"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dishCode: '',
          energy: '',
          protein: '',
          carbohydrate: '',
          fat: '',
          sugar: '',
          saturatedFat: '',
          transFat: '',
          cholesterol: '',
          sodium: ''
        },
        dataRule: {
          dishCode: [
            { required: true, message: '菜品编码不能为空', trigger: 'blur' }
          ],
          energy: [
            { required: true, message: '能量不能为空', trigger: 'blur' }
          ],
          protein: [
            { required: true, message: '蛋白质不能为空', trigger: 'blur' }
          ],
          carbohydrate: [
            { required: true, message: '碳水化合物不能为空', trigger: 'blur' }
          ],
          fat: [
            { required: true, message: '脂肪不能为空', trigger: 'blur' }
          ],
          sugar: [
            { required: true, message: '糖不能为空', trigger: 'blur' }
          ],
          saturatedFat: [
            { required: true, message: '饱和脂肪不能为空', trigger: 'blur' }
          ],
          transFat: [
            { required: true, message: '反式脂肪不能为空', trigger: 'blur' }
          ],
          cholesterol: [
            { required: true, message: '胆固醇不能为空', trigger: 'blur' }
          ],
          sodium: [
            { required: true, message: '钠不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/menu/nutrienttable/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dishCode = data.nutrientTable.dishCode
                this.dataForm.energy = data.nutrientTable.energy
                this.dataForm.protein = data.nutrientTable.protein
                this.dataForm.carbohydrate = data.nutrientTable.carbohydrate
                this.dataForm.fat = data.nutrientTable.fat
                this.dataForm.sugar = data.nutrientTable.sugar
                this.dataForm.saturatedFat = data.nutrientTable.saturatedFat
                this.dataForm.transFat = data.nutrientTable.transFat
                this.dataForm.cholesterol = data.nutrientTable.cholesterol
                this.dataForm.sodium = data.nutrientTable.sodium
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/menu/nutrienttable/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dishCode': this.dataForm.dishCode,
                'energy': this.dataForm.energy,
                'protein': this.dataForm.protein,
                'carbohydrate': this.dataForm.carbohydrate,
                'fat': this.dataForm.fat,
                'sugar': this.dataForm.sugar,
                'saturatedFat': this.dataForm.saturatedFat,
                'transFat': this.dataForm.transFat,
                'cholesterol': this.dataForm.cholesterol,
                'sodium': this.dataForm.sodium
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
